package com.adcc.loadsheet.billing.security;

import com.adcc.aoc.devLib.common.log.Log;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * Created by zhaoml on 2020/4/7.
 * @Description: 用户从上下文中获取所需的特殊业务权限信息
 */
@Component
public class BillingUserContext implements UserContext{

	//获取当前登录用户信息
	@Override
	public UserInfo getCurrentUser() {
		SecurityContext context = SecurityContextHolder.getContext();
		Authentication authentication = context.getAuthentication();
		if (authentication == null) {
			return null;
		}
		try{
			return (UserInfo) authentication.getPrincipal();
		}catch (Exception ex){
			Log.error(BillingUserContext.class, "获取当前登录用户信息出错", ex);
			return null;
		}
	}
}